from tortoise.models import Model
from tortoise import fields
import datetime

from config import BOOK_DATETIME_FORMAT


class Customer(Model):
    id = fields.IntField(pk=True, generated=True)
    name = fields.CharField(max_length=50, description="Customer Name")
    email = fields.CharField(max_length=50, default="None")
    phoneNumber = fields.CharField(max_length=14, default="None")
    address = fields.CharField(max_length=200)
    presenter = fields.CharField(max_length=30)
    password_hashed = fields.CharField(max_length=100, default="None")
    registerTime = fields.DatetimeField(auto_now_add=True)
    lastLoginTime = fields.DatetimeField(default=datetime.datetime.now(datetime.UTC))
    note = fields.TextField(default="None")
    avatar = fields.BinaryField(null=True)
    isDel = fields.BooleanField(default=False, description="Flag indicating if the customer record is deleted")

    def __repr__(self):
        return ("Customer(id={}, name={}, email={}, phoneNumber={}, address={}, presenter={}, password_hashed={}, registerTime={}, "
                "lastLoginTime={}, note={}, avatar={}, isDel={})").format(
            self.id, self.name, self.email, self.phoneNumber, self.address, self.presenter, self.password_hashed,
            self.registerTime, self.lastLoginTime, self.note, self.avatar, self.isDel
        )


class User(Model):
    """
    User model mapped to the 'user' table in the database.
    """
    id = fields.IntField(pk=True, generated=True)
    name = fields.CharField(max_length=50)
    email = fields.CharField(max_length=50, default="None")
    phoneNumber = fields.CharField(max_length=27, default='None')
    password_hashed = fields.CharField(max_length=255)
    registerTime = fields.DatetimeField(auto_now_add=True)
    role = fields.CharField(max_length=30, default='MANAGER')
    last_login_time = fields.DatetimeField(auto_now=True)
    note = fields.TextField(default='NULL')
    avatar = fields.BinaryField(default=None)
    isDel = fields.BooleanField(default=False)

    # class Meta:
    #     table = 'users'

    def __repr__(self):
        return "User(id={}, name={}, email={}, phoneNumber={}, password={}, registerTime={}, role={}, note={}, avatar={}, isDel={})".format(
            self.id, self.name, self.email, self.phoneNumber, self.password, self.registerTime, self.role,
                 self.note, self.avatar, self.isDel
        )


class Kumquat(Model):
    """
    Kumquat model mapped to the 'kumquat' table in the database.
    """
    id = fields.IntField(pk=True, generated=True)
    name = fields.CharField(max_length=50)
    style = fields.CharField(max_length=50)
    original_price = fields.DecimalField(max_digits=5, decimal_places=2, default=0)
    price = fields.DecimalField(max_digits=5, decimal_places=2, default=0)
    profit = fields.DecimalField(max_digits=5, decimal_places=2, default=0)
    price_date = fields.DatetimeField(auto_now=True, default=datetime.datetime.now(datetime.UTC).date())
    note = fields.TextField(default='None')

    # class Meta:
    #     table = 'kumquats'

    def __repr__(self):
        return "Kumquat(id={}, name={}, style={}, original_price={}, price={}, price_date={}, note={})".format(
            self.id, self.name, self.style, self.original_price, self.price, self.price_date, self.note
        )


class Order(Model):
    """
    Order model mapped to the 'order' table in the database.
    """
    id = fields.IntField(pk=True, generated=True)
    customer = fields.ForeignKeyField("models.Customer", related_name='orders')
    kumquat = fields.ForeignKeyField("models.Kumquat", related_name='kumquats')
    quantity = fields.IntField()
    on_sold = fields.DecimalField(max_digits=3, decimal_places=2, default=1)
    price = fields.DecimalField(max_digits=10, decimal_places=2, default=0)
    book_date = fields.DatetimeField(default=datetime.datetime.now().strftime(BOOK_DATETIME_FORMAT))
    status = fields.CharField(max_length=10, default='已下单')
    order_delivery_code = fields.CharField(max_length=20, default='None')
    note = fields.TextField(null=True, default='')

    def __repr__(self):
        return "Order(id={}, customer={}, kumquat={}, quantity={}, price={}, book_date={}, status={}, note={})".format(
            self.id, self.customer, self.kumquat, self.quantity, self.price, self.book_date, self.status,
            self.note
        )

    def get_price(self):
        price = self.kumquat.price * self.quantity * self.on_sold
        self.price = price if self.note.strip() != '赠送' else price*-1

    #
    # class Meta:
    #     table = 'order'